'use strict';

const BackgroundHelper = new function () {
	let mapCodeData = [
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	];
	const that = {
		mapCode: mapCodeData,
		// col
		x: mapCodeData.length,
		// row
		y: mapCodeData[0].length,
		draw: function (map) {
			for (let i = 0; i < that.x; i++) {
				for (let j = 0; j < that.y; j++) {
					if (mapCodeData[i][j] == 1) {
						Canvas.drawRect(map, j * 25, i * 25, 25, 25, '#000');
						Canvas.fillRect(GameManager.canvasList.back, j * 25 + 5, i * 25 + 5, 15, 15, 'black');
					} else {
						Canvas.drawRect(map, j * 25, i * 25, 25, 25, '#000');
					}
				}
			}
		},
		checkIsFullRow : function () {
			let rows = 0;

			for (let i = 0; i < mapCodeData.length; i++){
				if (this.calculateRowTotalValue(mapCodeData[i])){
					mapCodeData.remove(mapCodeData[i])
					let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
					mapCodeData.unshift(arr);
					rows++;
				}
			}

			if (!rows){
				return;
			}

			let score = 0;

			if (rows>=4){
				score = rows * 100 +600;
			}else if (rows>=3){
				score = rows * 100 +300;
			}else if (rows>=2){
				score = rows * 100 +100;
			}else{
				score = 100;
			}

			PlaneInfoManager.updateScore(score);
		},
		calculateRowTotalValue : function (arr) {
			let totalValue = 0;
			for (let i = 0; i < arr.length; i++){
				totalValue += arr[i];
			}
			return totalValue == arr.length;
		}
	};
	return that;
};